'use strict';

const Service = require('egg').Service;

class ArticleCategoryService extends Service {

  // 新增文章分类
  async createArticleCategory(ArticleCategory) {
    const { app } = this;
    return await app.model.ArticleCategory.create(ArticleCategory);
  }

  // 获取文章分类列表
  async getArticleCategory(pageNum, pageSize) {
    const { app } = this;
    try {
      const offset = (pageNum - 1) * pageSize;
      const { count, rows } = await app.model.ArticleCategory.findAndCountAll({
        include: {
          model: this.app.model.Article,
          as: 'articles',
        // attributes:['id','title','goods_price','goods_img'],
        },
        offset,
        limit: pageSize * 1,
      });
      return {
        pageNum,
        pageSize,
        total: count,
        list: rows,
      };
    } catch (error) {
      console.error(error);
      return null;
    }
  }

  // 单删除文章分类
  // async delArticleCategory(id) {
  //   const { app } = this;
  //   return await app.model.ArticleCategory.destroy({ where: { id } });
  // }

  // 批量删除 or 单删
  async del(payload) {
    const { app } = this;
    return await app.model.ArticleCategory.destroy({ where: { id: payload.ids }, force: true });
  }

  // // 根据id获取信息
  async getInfo(id) {
    const { app } = this;
    return await app.model.ArticleCategory.findByPk(id);
  }

  // 修改文章分类
  async updateArticleCategory(id, category) {
    const { app } = this;
    return await app.model.ArticleCategory.update(category, { where: { id } });
  }

}

module.exports = ArticleCategoryService;
